﻿define([
    'Wjs/ui/widgets/foundation/ViewSingleContainer',
    'Wjs/ui/elements/mixins/Disableable',
    'Wjs/ui/elements/mixins/Highlightable'
],
function () {
    'use strict';

    return Wjs.defineOnce({

        type: 'Button',

        internal: {

            base: Wjs.getType('ViewSingleContainer'), // Funcionally the button is not a field

            mixins: [
                Wjs.getType('Highlightable'),
                Wjs.getType('Disableable')
            ],

            initialize: function () {

                this.setTag('button');

                this.addClass('wjs-button wjs-selectable');

                this.addEvents({

                    click: function (evt) {

                        this.clickHandler(evt);
                    }

                });
            }

        },

        isSubmit: function (isSubmit) {

            this.submit = isSubmit;

            return this;
        },

        onClick: function (fcn) {

            this.clickHandler = Wjs.chain(this.clickHandler, fcn);

            return this;
        },

        clickHandler: function (evt) {

            if (this.submit) { // Submit the form when clicked

                evt.preventDefault();

                this.up(function (p) {

                    return p instanceof Wjs.getType('Form');
                })
                .submit();
            }
        },

        clone: function () {
            var b = Wjs.new('Button');

            if (this.getText()) {

                b.setText(this.getText());
            }

            //if (this.getHtml()) {

            //    b.setHtml(this.getHtml());
            //}

            b.setStyle(this.getStyle());

            b.clickHandler = this.clickHandler;

            return b;
        }
    });

});